package com.wskj.web.controller.workbench;

import java.util.List;
import java.util.Arrays;

import com.wskj.workbench.bo.web.TbUserReceiveOrderAddBo;
import com.wskj.workbench.bo.web.TbUserReceiveOrderEditBo;
import com.wskj.workbench.bo.web.TbUserReceiveOrderQueryBo;
import com.wskj.workbench.vo.web.TbUserReceiveOrderVo;
import lombok.AllArgsConstructor;

import javax.validation.constraints.*;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import com.wskj.common.annotation.RepeatSubmit;
import com.wskj.common.annotation.Log;
import com.wskj.common.core.controller.BaseController;
import com.wskj.common.core.domain.AjaxResult;
import com.wskj.common.enums.BusinessType;
import com.wskj.common.exception.CustomException;
import com.wskj.workbench.service.ITbUserReceiveOrderService;
import com.wskj.common.utils.poi.ExcelUtil;
import com.wskj.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

/**
 * 用户订单配送地址Controller
 *
 * @author wsdz
 * @date 2023-03-07
 */
@Api(value = "用户订单配送地址控制器", tags = {"用户订单配送地址管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/workbench/user/receive/order")
public class TbUserReceiveOrderController extends BaseController {

    private final ITbUserReceiveOrderService iTzUserReceiveOrderService;

    /**
     * 查询用户订单配送地址列表
     */
    @ApiOperation("查询用户订单配送地址列表")
    @PreAuthorize("@ss.hasPermi('workbench:order:list')")
    @GetMapping("/list")
    public TableDataInfo<TbUserReceiveOrderVo> list(@Validated TbUserReceiveOrderQueryBo bo) {
        return iTzUserReceiveOrderService.queryPageList(bo);
    }

    /**
     * 导出用户订单配送地址列表
     */
    @ApiOperation("导出用户订单配送地址列表")
    @PreAuthorize("@ss.hasPermi('workbench:order:export')")
    @Log(title = "用户订单配送地址", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated TbUserReceiveOrderQueryBo bo) {
        List<TbUserReceiveOrderVo> list = iTzUserReceiveOrderService.queryList(bo);
        ExcelUtil<TbUserReceiveOrderVo> util = new ExcelUtil<>(TbUserReceiveOrderVo.class);
        return util.exportExcel(list, "用户订单配送地址");
    }

    /**
     * 获取用户订单配送地址详细信息
     */
    @ApiOperation("获取用户订单配送地址详细信息")
    @PreAuthorize("@ss.hasPermi('workbench:order:query')")
    @GetMapping("/{receiveOrderId}")
    public AjaxResult<TbUserReceiveOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("receiveOrderId") Long receiveOrderId) {
        return AjaxResult.success(iTzUserReceiveOrderService.queryById(receiveOrderId));
    }

    /**
     * 新增用户订单配送地址
     */
    @ApiOperation("新增用户订单配送地址")
    @PreAuthorize("@ss.hasPermi('workbench:order:add')")
    @Log(title = "用户订单配送地址", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody TbUserReceiveOrderAddBo bo) {
        return toAjax(iTzUserReceiveOrderService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改用户订单配送地址
     */
    @ApiOperation("修改用户订单配送地址")
    @PreAuthorize("@ss.hasPermi('workbench:order:edit')")
    @Log(title = "用户订单配送地址", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody TbUserReceiveOrderEditBo bo) {
        return toAjax(iTzUserReceiveOrderService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除用户订单配送地址
     */
    @ApiOperation("删除用户订单配送地址")
    @PreAuthorize("@ss.hasPermi('workbench:order:remove')")
    @Log(title = "用户订单配送地址" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{receiveOrderIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] receiveOrderIds) {
        return toAjax(iTzUserReceiveOrderService.deleteWithValidByIds(Arrays.asList(receiveOrderIds), true) ? 1 : 0);
    }

    /**
     * 导入用户订单配送地址数据
     */
    @ApiOperation("导入用户订单配送地址数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('workbench:order:import')")
    @Log(title = "用户订单配送地址" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<TbUserReceiveOrderVo> util = new ExcelUtil<>(TbUserReceiveOrderVo.class);
        List<TbUserReceiveOrderVo> tzUserReceiveOrderList;
        try {
            tzUserReceiveOrderList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iTzUserReceiveOrderService.importTzUserReceiveOrder(tzUserReceiveOrderList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<TbUserReceiveOrderVo> util = new ExcelUtil<>(TbUserReceiveOrderVo.class);
        return util.importTemplateExcel("用户订单配送地址数据");
    }
}
